package com.neu.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.InputDevice;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.neu.R;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HdActivity extends AppCompatActivity {

    private EditText inputweb;
    private TextView webcontent;



    //
    public static class MyHandler extends Handler{
        private final WeakReference<HdActivity> mWeakActivity;

        public MyHandler(HdActivity activity){
            mWeakActivity=new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            //获得activity
            HdActivity activity=mWeakActivity.get();

            //处理子线程来的数据
            switch (msg.what){
                case 1:{
                    //获得组件的使用
                    activity.webcontent.setText(msg.obj.toString());
                    break;
                }
            }
        }
    }

    private MyHandler myHandler;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        myHandler.removeCallbacksAndMessages(null);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hd);

        //获得引用
        inputweb=findViewById(R.id.inputweb);
        inputweb.setText("https://www.baidu.com");
        webcontent=findViewById(R.id.webcontent);
        myHandler=new MyHandler(this);

    }

    //网络链接代码
    public void btnIntent(View view) {
        //这是一个线程，并启动了
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url=new URL(inputweb.getText().toString().trim());
                    HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setConnectTimeout(5000);
                    int code=urlConnection.getResponseCode();

                    if(code==200){
                        //获得输入流
                        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                      //  webcontent.setText(in.toString());
                        int len=0;
                        byte[] buf=new byte[1024];
                       final ByteArrayOutputStream baos=new ByteArrayOutputStream();
                        while ((len=in.read(buf,0,1024))>0){
                            baos.write(buf,0,len);
                        }
                        //在子线程中，去更新了UI,是不允许的
                       // webcontent.setText(baos.toString());

//                        runOnUiThread(new Runnable() {
//                            @Override
//                            public void run() {
//                                webcontent.setText(baos.toString());
//                            }
//                        });
                      //  Message msg=new Message();
                        Message msg=myHandler.obtainMessage();
                        //如果是1成功的数据
                        msg.what=1;
                        //将数据放入到msg中
                        msg.obj=baos.toString();
                        myHandler.sendMessage(msg);
                    }

                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();





    }
}